package com.xltt.watchclient.module;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
import android.os.ParcelUuid;

import com.xltt.watchclient.LogTool;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import androidx.lifecycle.MutableLiveData;

public class ScanManager {
    public static UUID UUID_SERVER = UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb");
    public static UUID UUID_CHARREAD = UUID.fromString("0000fff1-0000-1000-8000-00805f9b34fb");
    public static UUID UUID_CHARWRITE = UUID.fromString("0000fff2-0000-1000-8000-00805f9b34fb");
    Context mContext;
    private BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

    public ScanManager(Context mContext) {
        this.mContext = mContext;
    }

    BluetoothLeScanner scanner;
    MutableLiveData<Map<String, BluetoothDevice>> scanResults;
    boolean isRunning;

    public boolean start(ScanCallback scanCallback) {
        if (!isRunning) {
            scanResults = new MutableLiveData<>();
            scanner = adapter.getBluetoothLeScanner();
            if (scanner!=null) {
                scanner.startScan(buildScanFilters(), buildScanSettings(), scanCallback);
                isRunning = true;
            }
        }
        return isRunning;
    }

    public boolean stop(ScanCallback scanCallback) {
        if (isRunning) {
            isRunning = false;
            scanner.stopScan(scanCallback);
        }
        return !isRunning;
    }


    /**
     * Return a List of [ScanFilter] objects to filter by Service UUID.
     */
    private List<ScanFilter> buildScanFilters() {
        ScanFilter.Builder builder = new ScanFilter.Builder();
        // Comment out the below line to see all BLE devices around you
        builder.setServiceUuid(new ParcelUuid(UUID_SERVER));
        ScanFilter filter = builder.build();
        return Arrays.asList(filter);
    }

    /**
     * Return a [ScanSettings] object set to use low power (to preserve battery life).
     */
    private ScanSettings buildScanSettings() {
        return new ScanSettings.Builder()
                .setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)
                .build();
    }


//    private class DeviceScanCallback extends ScanCallback {
//
//        @Override
//        public void onScanResult(int callbackType, ScanResult result) {
//            super.onScanResult(callbackType, result);
//            if (result.getDevice() != null) {
//                _scanResults.put(result.getDevice().getAddress(), result.getDevice());
//            }
//            LogTool.d(""+result.getDevice());
//            scanResults.postValue(_scanResults);
//        }
//
//        @Override
//        public void onBatchScanResults(List<ScanResult> results) {
//            super.onBatchScanResults(results);
//        }
//
//        @Override
//        public void onScanFailed(int errorCode) {
//            super.onScanFailed(errorCode);
//            // Send error state to the fragment to display
//            String errorMessage = "Scan failed with error: " + errorCode;
//            scanResults.postValue(null);
//        }
//    }
}
